import request from '@/utils/request'
import { Effect, Reducer } from 'umi';

export interface ModelType {
    namespace: string;
    state: any;
    effects: {
        fetchList: Effect;
        getSearchParams: Effect;
    };
    reducers: {
        save: Reducer<{}>;
        saveSearchParams: Reducer<{}>;
    };
}

const model: ModelType = {
    namespace: "goods",
    state: {
        tableData: {
            list: [],
            pagination: {},
        },
        searchParams: {
            brandList: [],
            tagList: [],
            categoryTrees: [],
        }
    },
    effects: {
        *fetchList({ payload }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    list: [],
                    pagination: { total: 0 }
                },
            });
            const response = yield call(
                async (params: any) => {
                    return request('/goods/list', {
                        params
                    })
                }, payload)
            yield put({
                type: 'save',
                payload: {
                    list: response.data.list,
                    pagination: { total: response.data.total }
                },
            });
        },
        *getSearchParams({ payload }, { call, put }) {
            const response = yield call(
                async (params: any) => {
                    return request('/goods/list-search-params')
                }, payload)
            console.log(response.data);
            yield put({
                type: 'saveSearchParams',
                payload: response.data,
            });
        }
    },
    reducers: {
        save(state, action) {
            return {
                ...state,
                tableData: action.payload,
            };
        },
        saveSearchParams(state, action) {
            return {
                ...state,
                searchParams: action.payload,
            };
        }
    }
}
export default model